package org.apache.cordova.getIntent;
import java.io.File;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaActivity;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.LOG;
import org.json.JSONArray;
import org.json.JSONException;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.inputmethod.InputMethodManager;
import cn.clickmed.DaTong.R;
import cn.clickmed.DaTong.DaTong;
import cn.clickmed.Top.Top;
import cn.clickmed.exitApp.SysApplication;
import cn.clickmed.getActivity.getActivity;
import cn.clickmed.getActivityB.getActivityB;
import cn.clickmed.getActivityC.getActivityC;
import cn.clickmed.getActivityG.getActivityG;




public class getIntent extends CordovaPlugin{
	@Override  
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {  
        if ("getIntent".equals(action)) {  
        	Intent intent=(this.cordova.getActivity()).getIntent();
        	 Bundle extras =intent.getExtras();
         	hide();
        	
        	String str =extras.getString("businessType");
        	String url =extras.getString("url");
        	
		   callbackContext.success(str+url);
		            return true;  
		        }
		        else if ("setIntent".equals(action)){
		        //	Context contex =DaTong.getContext();
		       // 	InputMethodManager imm = (InputMethodManager)getSystemService(contex.INPUT_METHOD_SERVICE);
		        //	hide();
		        	 InputMethodManager mgr = (InputMethodManager) cordova.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
		   		     mgr.hideSoftInputFromWindow(webView.getView().getWindowToken(),  0 );
		   		//  Log.e("mgr","hidded");
		        	 Intent tempI=this.cordova.getActivity().getIntent();
		        	 tempI.putExtra("businessType", "ttttFFFFFFFF");
		        	 
		        	return true;
		        }
		        else if("getVersion".equals(action)){
		        	String verName = this.cordova.getActivity().getResources()  
		                    .getText(R.string.app_version).toString();
		        	 callbackContext.success(verName);
		        	return true;
		        }
		        else if("install".equals(action)){
		        	String fileName=args.getString(0);
		        	int len =fileName.lastIndexOf("/download_test");
		        	 File file = new File(Environment.getExternalStorageDirectory()+fileName.substring(len));
		     //  	 Log.e("fileName", Environment.getExternalStorageDirectory()+fileName.substring(len));

		        	Intent intent = new Intent();
		        	intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		        	intent.setAction(Intent.ACTION_VIEW);
		        	intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
		        	
		        	this.cordova.getActivity().startActivity(intent);

		        	return true;
		        }
		        else if("openView".equals(action)){
		        	String url=args.getString(0);
		        	hide();
		       
		        	Intent intent =new Intent(cordova.getContext(), getActivity.class);
		        	intent.putExtra("url",url);
		        	intent.setAction("android.intent.action.VIEW");
		        
		        	
		        	LOG.e("Intent",url);
		        //	intent.setClassName("com.android.browser", "cn.clickmed.DaTong.DaTong"); 
		        	this.cordova.getActivity().startActivity(intent); 
		        	//this.cordova.getActivity().startActivity(new Intent(Intent.ACTION_VIEW,uri)); 
		        	return true;
		        }
		        else if("openViewB".equals(action)){
		        	String url=args.getString(0);
		        	Intent intent =new Intent(cordova.getContext(), getActivityB.class);
		        	intent.putExtra("url",url);
		        	intent.setAction("android.intent.action.VIEW");
		        	this.cordova.getActivity().startActivityForResult(intent,9); 
		        	/*this.cordova.getActivity().overridePendingTransition(R.anim.in_left,
		        			R.anim.out_right);*/
		        	return true;
		        }
		        else if("openViewC".equals(action)){
		        	String url=args.getString(0);
		        	Intent intent =new Intent(cordova.getContext(), getActivityC.class);
		        	intent.putExtra("url",url);
		        	intent.setAction("android.intent.action.VIEW");
		        	this.cordova.getActivity().startActivityForResult(intent,9); 
		        	return true;
		        }
		        else if("openViewG".equals(action)){
		        	String url=args.getString(0);
		        	Intent intent =new Intent(cordova.getContext(), getActivityG.class);
		        	intent.putExtra("url",url);
		        	intent.setAction("android.intent.action.VIEW");
		        	this.cordova.getActivity().startActivityForResult(intent,9); 
		        	return true;
		        }
		        else if("closeView".equals(action)){
		        	
		        	this.cordova.getActivity().finish();
		        	
		        	return true;
		        }
		        else if("exit".equals(action)){
		        	//Intent intent=new Intent(Intent.ACTION_MAIN);
		        	//intent.addCategory(Intent.CATEGORY_HOME);
		        	//intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		        	//this.cordova.getActivity().startActivity(intent);
		        	SysApplication.getInstance().exit();  
		        	//System.exit(0);
		        	return true;
		        }
		        else if("startTop".equals(action)){
		        	Intent intent =new Intent(cordova.getContext(), Top.class);
		        	intent.putExtra("url", "file:///android_asset/www/views/login/login.html");
		        	intent.setAction("android.intent.action.VIEW");
		        	SysApplication.getInstance().exit(); 
		        	this.cordova.getActivity().startActivity(intent);
		        	return true;
		        	
		        }
		        else  {  
		            return false;  
		        }
		 
		    }

	private void hide() {
		// TODO Auto-generated method stub
		InputMethodManager mgr = (InputMethodManager) cordova.getContext().getSystemService(
				Context.INPUT_METHOD_SERVICE);
		  mgr.hideSoftInputFromWindow(webView.getView().getWindowToken(),  0 );
		
	}


}
